import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const Layout = () => import('../views/Layout.vue')
const Login = () => import('../views/Login.vue')
const Index = () => import('../components/Index.vue')
const Classes = () => import('../components/Classes.vue')
const ClassInfo = () => import('../components/ClassInfo.vue')

const routes = [
  {
    path:"/",
    redirect: "/index"
  },
  {
    path:"/layout",
    component: Layout,
    children:[
      {
        path:"/index",
        component: Index,
        meta: {
          title: '首页'
        },//元
      },
      {
        path:"/classes",
        component: Classes,
        meta: {
          title: '班级列表'
        }
      },
      {
        path:"/class_info/:id",
        component: ClassInfo,
        meta: {
          title: '班级详情'
        }
      },
      
    ]
  },
  {
    path:"/login",
    component: Login,
    meta: {
      title: '登录'
    }
  },
]

const router = new VueRouter({
  routes,
  scrollBehavior(){
    //函数拥有 to和from参数
    //可以通过 return {x: 0,y: 0}来控制滚动条
    console.log(arguments)
    return {x: 0,y: 0}
  }
})

router.beforeEach((to,from,next)=>{
  //如果不是登录页面，必须有登录标志才可以登录
  if(to.path!=="/login"){
    //判断登录标志
    const isLogin = localStorage.getItem("islogin")
    if(isLogin && isLogin === "1"){
      return next()
    }else{
      return next("/login")
    }
  }
  next()
})

//导出route对象之前
export default router
